<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">

<!-- 内容主体区域 -->
<div class="layui-row layui-pad15">
    <form class="layui-form" action="">
            <div class="layui-col-md12 layui-col-lh">
                <div class="layui-row ">
                     <div class="layui-col-xs3">
                        类型名称
                     </div>
                     <div class="layui-col-xs8">
                         <input id="typeName" type="text" name="typeName" placeholder="类型名称" class="form-control">
                     </div>
                </div>
            </div>
            <div class="layui-col-md12  layui-text-center layui-col-lh">
                <!--<a href="#" id="cancelBtn" class="layui-q-c layui-t-can layui-dels  layui-btn layui-btn-primary">取消</a>-->
                <a href="#" id="okBtn" class="layui-q-c layui-zdy-bj">确定</a>
            </div>
    </form>

</div>
<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>
$(function () {
    let getPaperTypeUrl = baseUrl + "/exam/paper-type";
    let createPaperTypeUrl = baseUrl + "/exam/paper-type";
    let updatePaperTypeUrl = baseUrl + "/exam/paper-type";
    let getQuestionTypeUrl = baseUrl + "/exam/getQuestionTypeById";
    let createQuestionTypeUrl = baseUrl + "/exam/createQuestionType";
    let updateQuestionTypeUrl = baseUrl + "/exam/updateQuestionType";
    // ?processingType=2&processingId=1的顺序格式（processingType：1新增问卷类型，2编辑问卷类型，3新增问题类型，4编辑问题类型；processingId：类型id）
    let url = location.search;
    let processingType = 0;
    // 修改的数据的id，提交时需大于0
    let processingId = 0;
    // 取消事件
    $("#cancelBtn").on("click", function () {
        closeWindow();
    });
    if(url.indexOf("?") != -1) {
        let params = url.substr(1);
        let paramArray = params.split("&");
        if (paramArray && paramArray.length >= 1) {
            processingType = paramArray[0].split("=")[1];
        }
        if (paramArray && paramArray.length >= 2) {
            processingId = paramArray[1].split("=")[1];
        }
        if (processingType && processingType >= 1 && processingType <= 4 &&
            ((processingId && processingId > 0) || processingType == 1 || processingType == 3)) {
            // 确定事件
            $("#okBtn").on("click", function () {
                if (processingType == 1) {
                    // 1新增问卷类型
                    if (getTypeName()) {
                        createPaperType();
                    } else {
                        layer.msg("请填写名称");
                    }
                } else if (processingType == 2) {
                    // 2编辑问卷类型
                    if (getTypeName() && processingId > 0) {
                        updatePaperType();
                    } else {
                        layer.msg("请填写名称");
                    }
                } else if (processingType == 3) {
                    // 3新增问题类型
                    if (getTypeName()) {
                        createQuestionType();
                    } else {
                        layer.msg("请填写名称");
                    }
                } else if (processingType == 4) {
                    // 4编辑问题类型
                    if (getTypeName() && processingId > 0) {
                        updateQuestionType();
                    } else {
                        layer.msg("请填写名称");
                    }
                }
            });

            // 初始化数据
            if (processingType == 1) {
                // 1新增问卷类型
                changeTypeName("");
            } else if (processingType == 2) {
                // 2编辑问卷类型
                getPaperTypeUrl = getPaperTypeUrl + "/" + processingId;
                $.ajax({
                    type: "GET",
                    url: getPaperTypeUrl,
                    success: function (msg) {
                        if (msg && msg.state == 1 && msg.data) {
                            changeTypeName(msg.data.typeName);
                        } else {
                            layer.msg("获取类型失败");
                        }
                    },
                    error: function () {
                        layer.msg("获取类型失败");
                    }
                });
            } else if (processingType == 3) {
                // 3新增问题类型
                changeTypeName("");
            } else if (processingType == 4) {
                // 4编辑问题类型
//                getQuestionTypeUrl = getQuestionTypeUrl + "/" + processingId;
                $.ajax({
                    type: "GET",
                    url: getQuestionTypeUrl,
                    data:{questionTypeId : processingId},
                    success: function (msg) {
                        if (msg && msg.state == 1 && msg.data) {
                            changeTypeName(msg.data.typeName);
                        } else {
                            layer.msg("获取类型失败");
                        }
                    },
                    error: function () {
                        layer.msg("获取类型失败");
                    }
                });
            }
        }
    }

    // 创建问卷类型
    function createPaperType() {
        $.ajax({
            type: "POST",
            url: createPaperTypeUrl,
            data: JSON.stringify({
                "typeName": getTypeName()
            }),
            headers: {"content-type": "application/json"},
            success: function (msg) {
                if (msg && msg.state == 1 && msg.data) {
                    closeWindow(function() {
                        window.parent.layer.msg("创建类型成功");
                        window.parent.doSearch("current");
                    });
                } else {
                    layer.msg("创建类型失败");
                }
            },
            error: function () {
                layer.msg("创建类型失败");
            }
        });
    }

    // 更新问卷类型
    function updatePaperType() {
        $.ajax({
            type: "PUT",
            url: updatePaperTypeUrl,
            data: {
                "paperTypeId": processingId,
                "typeName": getTypeName()
            },
            success: function (msg) {
                if (msg && msg.state == 1 && msg.data) {
                    closeWindow(function() {
                        window.parent.layer.msg("更新类型成功");
                        window.parent.doSearch("current");
                    });
                } else {
                    layer.msg("更新类型失败");
                }
            },
            error: function () {
                layer.msg("更新类型失败");
            }
        });
    }

    // 创建问题类型
    function createQuestionType() {
        $.ajax({
            type: "POST",
            url: createQuestionTypeUrl,
            data: JSON.stringify({
                "typeName": getTypeName()
            }),
            headers: {"content-type": "application/json"},
            success: function (msg) {
                if (msg && msg.state == 1 && msg.data) {
                    closeWindow(function() {
                        window.parent.layer.msg("创建类型成功");
                        window.parent.doSearch("current");
                    });
                } else {
                    layer.msg("创建类型失败");
                }
            },
            error: function () {
                layer.msg("创建类型失败");
            }
        });
    }

    // 更新问题类型
    function updateQuestionType() {
        $.ajax({
            type: "POST",
            url: updateQuestionTypeUrl,
            data: JSON.stringify({
                "id": processingId,
                "typeName": getTypeName()
            }),
            headers: {"content-type": "application/json"},
            dataType:"JSON",
            success: function (msg) {
                if (msg && msg.state == 1 && msg.data) {
                    closeWindow(function() {
                        window.parent.layer.msg("更新类型成功");
                        window.parent.doSearch("current");
                    });
                } else {
                    layer.msg("更新类型失败");
                }
            },
            error: function () {
                layer.msg("更新类型失败");
            }
        });
    }

    // 更新表单类型名称
    function changeTypeName(typeName) {
        $("#typeName").val(typeName);
    }

    // 获取表单类型名称
    function getTypeName() {
        return $("#typeName").val();
    }

    // 关闭窗口
    function closeWindow(callback) {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
        if(typeof callback == 'function'){
            callback();
        }
    }
});

layui.use(['layer'], function () {
});
</script>


